package com.jrx.anytxn.common.data;

import java.util.Arrays;

/**
 * 统一返回码枚举对象
 *
 * @author zhaowg
 * @date 2018-08-03 17:45
 **/

public enum TxnRespCode {

    /**
     * 默认的成功操作状态码
     */
    SUCCESS(1000, "成功"),
    ERROR(9999, "操作失败", "操作失败：9999"),


    NO_DATA(2000, "数据未找到", "数据未找到：2000"),
    DATA_EXISTS(2001, "数据已存在", "数据已存在：2001"),
    NOT_EMPTY(2002, "参数不能为空", "参数不能为空：2002"),
    BIZ_ERROR(2003, "业务异常", "业务异常：2003"),
    PARAM_ERROR(2004, "参数错误", "参数错误：2004"),

    // 交易类型业务异常2010 - 2019
    TRANSACTION_TYPE_EXISTS(2010, "交易类型已存在", "交易类型已存在：2010"),
    TRANSACTION_TYPE_ID_EMPTY(2011, "交易类型ID不能为空", "交易类型ID不能为空：2011"),
    TRANSACTION_TYPE_NOT_FOUND(2012, "交易类型未找到", "交易类型未找到：2012"),
    TRANSACTION_CODE_EXISTS(2013, "交易码已存在", "交易码已存在：2013"),
    TRANSACTION_CODE_ID_EMPTY(2014, "交易码ID不能为空", "交易码ID不能为空：2014"),
    TRANSACTION_CODE_NOT_FOUND(2015, "交易码未找到", "交易码未找到：2015"),
    TRANSACTION_ORDER_CANCLE(2016, "交易已取消", "交易已取消：2016"),
    TRANSACTION_PROCESSING(2017, "客户有正在处理的交易，请稍后再试"),
    CARD_BAND_ERROR(2018, "客户卡已被绑定","操作失败：2018"),
    CARD_IS_BANDING_ERROR(2019, "客户有未完成的绑卡交易","操作失败：2019"),
    GET_SEGMENT_ID_ERROR(2005,"ID 生成异常"),
    TRAN_AMT_NULL(2020, "交易金额不能为空", "交易金额不能为空"),
    TRAN_AMT_LESS_THEN_99(2021,"交易金额总金额小于99元", "交易金额总金额小于99元"),
    TRAN_AMT_LESS_THEN_299(2022,"交易金额总金额小于299元", "交易金额总金额小于299元"),
    TRAN_AMT_LESS_THEN_499(2023,"交易金额总金额小于499元", "交易金额总金额小于499元"),
    TRAN_AMT_LESS_THEN_999(2024,"交易金额总金额小于999元", "交易金额总金额小于999元"),

    MOBILE_MODIFY_ERROR(2025,"手机号修改失败", "手机号修改失败"),
    ACTIVITY_NOT_OPEN(2030,"活动尚未开启","活动尚未开启"),
    ACTIVITY_SUM_TRAN_AMT_NOT_REACH_WARN(2031,"消费还差%s元达标，五折券抢兑时间 周五10:00～周日23:59","消费还差%s元达标，五折券抢兑时间 周五10:00～周日23:59"),
    ACTIVITY_SUM_TRAN_AMT_REACH_TIME_NOT_WARN(2032,"消费已达标  五折券抢兑时间周五10:00～周日23:59","消费已达标  五折券抢兑时间周五10:00～周日23:59"),
    ACTIVITY_COME_ON(2033,"抱歉，消费未达标，下周再接再厉哦！","抱歉，消费未达标，下周再接再厉哦！"),
    TRY_LOCK_FAIL(2034,"当前人数太多,请稍后重试","当前人数太多,请稍后重试"),
    TRY_LOCK_AGAIN_FAIL(2035,"当前人数太多了,请稍后重试","当前人数太多了,请稍后重试"),

    GET_CUSTOMER_NULL(2040,"该手机号未激活兴安花"),
    GET_CUSTOMER_ERROR(2041,"该手机号绑定了多条客户信息"),

    //还款相关 3001-->3099
    PAYMENT_LOG_NOT_FOUND(3001, "还款->未找到还款流水", "操作失败：3001"),
    PAYMENT_ADVANCE_SETTLE_NOT_ALLOW(3002, "提前结清->不允许提前结清", "不允许提前结清"),
    PAYMENT_LOAN_STATUS_NOT_ALLOW(3003, "还款->订单在当前状态下不允许还款", "订单在当前状态下，不允许还款"),
    PAYMENT_LOANING_NOT_ALLOW(3004, "还款->订单在放款处理中，不允许还款", "订单放款处理中，不允许还款"),
    PAYMENT_LOAN_DAY_NOT_ALLOW(3005, "还款->借款日当天不许还款", "借款日当天不允许还款，明天再来吧"),
    PAYMENT_BATCH_PROCESSING_NOT_ALLOW(3006, "还款->批次处理中不许还款", "订单处理中不允许还款，请稍后再试"),
    PAYMENT_NOT_PAYMENT_DAY_NOT_ALLOW(3007, "正常还款->非还款日当天不允许还款", "非还款日，不允许还款"),
    PAYMENT_PAYMENT_DAY_NOT_ALLOW(3008, "提前还款->还款日当天不允许提前还款", "还款日当天不允许提前还款，请等待系统自动扣款"),
    PAYMENT_LOAN_OVER_DUE_NOT_ALLOW(3009, "提前(正常)还款->订单逾期不允许提前(正常)还款", "逾期订单不允许提前还款，请操作逾期还款"),
    PAYMENT_PROCESSING_NOT_ALLOW(3010, "还款->订单处于还款中，不允许还款", "订单还款处理中，请勿操作"),
    PAYMENT_AUTO_PAYMENT_PROCESSING_NOT_ALLOW(3011, "还款->批量扣款中，不允许还款", "订单扣款处理中，请勿操作"),
    PAYMENT_DEBT_AMOUNT_ZERO(3012, "还款->订单[%s]没有欠款，欠款额[%s]", "此订单已结清"),
    PAYMENT_AMOUNT_ZERO(3013, "还款->订单还款额[%s]小等于0，不允许还款", "此订单已结清"),
    PAYMENT_PRIN_AMOUNT_WRONG(3014, "提前还款本金[%s]大于欠款本金[%s]，不允许还款", "还款本金不能大于欠款本金"),
    PAYMENT_AMOUNT_LESS_MIN(3015, "还款后的剩余本金[%s]小于最小产品允许的剩余本金[%s]，不允许还款", "还款后的剩余本金[%s]不能小于[%s]"),
    PAYMENT_MISTAKE_EXIST(3016, "存在差错记录，不允许还款", "操作失败：3016"),
    PAYMENT_NOT_OVER_DUE_NOT_ALLOW(3017, "逾期还款->订单未逾期，不允许还款", "逾期还款->订单未逾期，不允许还款"),
    PAYMENT_AMOUNT_OVER_DEBT_NOT_ALLOW(3018, "逾期还款额[%s]大于欠款额[%s]，不允许还款", "还款金额不能大于欠款金额"),
    PAYMENT_NOT_STAGING_DAY_ALLOW(3019, "账单分期->业务日期不满足分期条件", "暂不满足账单分期条件"),
    PAYMENT_BILLOVER_AMOUNT_WRONG(3020, "账单逾期还款金额[%s]大于欠款金额[%s]，不允许还款", "还款金额不能大于欠款金额"),
    PAYMENT_BILL_NOT_READY(3021, "当期账单未出，不能提前还款", "未出账单不能还款"),
    PAYMENT_BILL_AMOUNT_WRONG(3022, "账单还款金额[%s]不正确，不允许还款", "账单还款金额不正确"),
    PAYMENT_PRINCIPAL_AMOUNT_WRONG(3023, "提前还款-> 还款本金与还款总金额 必须且只能传一个值", "操作失败：3023"),
    PAYMENT_TOTAL_AMOUNT_WRONG(3024, "提前还款总金额[%s]大于欠款总金额[%s]，不允许还款", "还款金额不能大于欠款金额"),
    PAYMENT_TOTAL_DEBT_AMOUNT_WRONG(3025, "提前还款总金额[%s]小于当前欠款金额[%s]，不允许还款", "还款金额需等于欠款金额"),
    PAYMENT_STAGING_EXIST(3026, "存在账单分期记录，不允许账单分期", "该账单已分期，请勿重复操作"),
    PAYMENT_TERM_NO_DATA(3027, "第三方还款>指定期次，代偿还款期次不能为空或小于0"),
    PAYMENT_LOAN_NO_DATA(3028, "第三方还款>合同号，外部合同号不能都为空"),
    PAYMENT_LOAN_NO_CLEAN(3029, "第三方还款>订单之前的期次未结清"),
    PAYMENT_PAYMENT_TYPE_NOT_ALLOW(3030, "第三方还款>不支持的还款类型"),
    PAYMENT_LOAN_IS_EMPTY(3031, "不存在可以还款的订单"),
    PAYMENT_PREPAY_NOT_ALLOW(3032, "提前还款->不允许提前还款", "不允许提前还款"),
    PAYMENT_CUSTOMER_BATCH_ERROR(3033,"客户日终账户处理异常","客户日终账户处理异常"),
    PAYMENT_REPAY_AMOUNT_ERROR(3034,"主动还款金额需大于[%s]","主动还款金额需大于[%s]"),
    PAYMENT_REPAY_EXCEPTIONTXN_ERROR(3035,"还款发生异常","还款发生异常"),
    PAYMENT_BILL_OVER(3036, "当期账单逾期，不能提前还下期", "当期账单逾期，不能提前还下期"),
    PAYMENT_MIN_AMT(3037, "当期账单分期金额不满足产品最小金额", "当期账单分期金额不满足产品最小金额"),
    PAYMENT_NOT_ALLOW_TRANS_STAGING(3038, "交易已出账单不允许交易分期", "交易已出账单不允许交易分期"),
    CUSTOMER_INFO_NOT_EXIST(3039,"客户不存在","客户不存在"),
    PAYMENT_MONTH_IS_ERROR(3040,"账单月份错误","账单月份错误"),
    ETL_SETTLE_DATE_ERROR(3041,"获取结清日期失败","获取结清日期失败"),
    REFUND_ERROR(3042,"退款失败","退款失败"),
    PAYMENT_STAGING_TIME(3043,"该时间段不允许分期","该时间段不允许分期"),

    //贷款相关 3100-3099
    LOAN_PRODUCT_AMORT_METHOD_NOT_EXIST(3101,"产品不具有该还款方式","操作失败：3101"),
    LOAN_GT_MAX_AMOUNT(3102,"贷款金额超过产品最大允许金额","贷款金额不能超过%s"),
    LOAN_LT_MIN_AMOUNT(3103,"贷款金额低于产品最小允许金额","贷款金额不能小于%s"),
    LOAN_ALLOW_TENOR(3104,"贷款期数不在产品允许范围","不支持选择该贷款期数"),
    LOAN_PRODUCT_LOAN_TIME_NONE(3105,"产品未配置贷款时长","操作失败：3105"),
    LOAN_DATE_ERROR(3106,"放款日期[%s]大于当前业务日期[%s]，不允许贷款","操作失败：3106"),
    LOAN_ORDER_CANCEL(3107, "交易已取消","操作失败：3107"),
    LOAN_ORDER_CLEAR(3108, "订单已经结清","此订单已结清"),
    PRODUCT_TYPE_NOT_EXIST(3109, "没有上传产品码时，贷款类型不可空","操作失败：3109"),
    CONTRACTNUMBER_IS_EXIST(3110,"合同号已存在"),
    MERCHANT_NOT_EXIT(3111,"商户不存在","商户不存在"),
    LOAN_REMIT_WAY_NOT(3112,"不支持的放款路径","只支持放款到指定账户"),
    LOAN_REMIT_ACCOUNT_NOT(3113,"不支持的放款账户类型","不支持的放款账户类型"),
    CANCEL_LOAN_STATUS_NOT_ALLOW(3114, "取消订单->订单在当前状态下不允许取消", "订单在当前状态下，不允许取消"),
    CANCEL_REMIT_WHEN_LOAN_NOT_ALLOW(3115, "取消订单->只支持非下单即放款的订单", "只支持非下单即放款的订单"),
    LOAN_GT_FARTNER_AVAILABLE_LIMIT(3116,"贷款金额超过合作方贷款总可用额度","贷款金额不能超过%s"),
    LOAN_GT_FARTNER_DAILY_AVAILABLE_LIMIT(3117,"贷款金额超过合作方贷款当日可用额度","贷款金额不能超过%s"),
    CUSTOMER_RIGHT_NOT_EXIT(3118,"优惠券不存在","优惠券不存在"),
    CUSTOMER_RIGHT_ALREADY_USED(3119,"优惠券已被使用","优惠券已被使用"),
    CUSTOMER_RIGHT_DATE_NOT_RIGHT(3120,"优惠券不在有效使用时间范围内","优惠券不在有效使用时间范围内"),
    CUSTOMER_RIGHT_PRODUCT_NOT_RIGHT(3121,"优惠券不在可用产品范围内","优惠券不在可用产品范围内"),
    CUSTOMER_RIGHT_TENOR_NOT_RIGHT(3122,"优惠券不在符合借款期数范围内","优惠券不在符合借款期数范围内"),
    CUSTOMER_RIGHT_AMT_NOT_RIGHT(3123,"优惠券不在有效借款金额范围内","优惠券不在有效借款金额范围内"),
    LOAN_IS_NOT_FAIL(3124, "订单未放款失败","订单未放款失败"),
    LOAN_IS_NOT_CANCEL(3125, "原订单未取消","原订单未取消"),
    COUPON_IN_ACTIVITY_ALREADY_EXISTS(3126,"此活动优惠券已下发","此活动优惠券已下发"),
    COUPON_NOT_EXIST(3127,"优惠券不存在","参数表优惠券不存在"),
    QUALIFICATION_EXCHANGE_REQ_ERR(3128,"权益id、资格id都不为空","权益id、资格id都不为空"),
    COUPON_TYPE_MISSING(3129,"权益下发权益类型不能为空","权益下发权益类型不能为空"),
    COUPON_NOT_ENABLE(3120,"权益未启用","权益未启用"),
    RIGHTS_QUALIFICATION_NOT_ENABLE(3121,"权益资格未启用","权益资格未启用"),
    COUPON_QUALIFICATION_IN_ACTIVITY_ALREADY_EXISTS(3122,"此活动权益资格已下发","此活动权益资格已下发"),
    QUALIFICATION_NOT_GRANTED(3123, "未有足够待使用的权益资格,无法兑换", "未有足够待使用的权益资格,无法兑换"),
    PRODUCT_CODE_AND_RIGHTS_ID_MISMATCH(3124, "请求产品编号与权益id适用产品不匹配", "请求产品编号与权益id适用产品不匹配"),
    PRODUCT_CODE_AND_QUALIFICATION_ID_MISMATCH(3124, "请求产品编号与权益资格id适用产品不匹配", "请求产品编号与权益资格id适用产品不匹配"),
    ACTIVITY_STOCK_DEFICIENT(3310, "活动派发权益库存不足","活动派发权益库存不足"),
    RIGHTS_AND_INTEREST_STOCK_DEFICIENT(3311, "权益库存不足","权益库存不足"),
    RIGHTS_EXCHANGE_OVER_DEFICIENT(3311, "该权益本期已抢兑完，请选择其他权益进行兑换","该权益本期已抢兑完，请选择其他权益进行兑换"),
    ACTIVITY_STOCK_RATIO_NONE(3312,"活动库存比例不能为空","活动库存比例不能为空"),
    ACTIVITY_NOT_BEGIN(3313,"活动未到生效时间","活动未到生效时间"),
    ACTIVITY_EXPIRE(3314,"活动已过期","活动已过期"),
    COUPON_EXPIRATION(3315,"权益已失效","权益已失效"),
    EXCHANGE_ACTIVITY_NOT_BEGIN(3316,"权益兑换开始时间为：每月%s", "权益兑换开始时间为：每月%s"),

    ACTIVITY_NOT_EXIST(4000,"活动不存在","活动不存在"),
    //4001、4002会触犯前端登出拦截 禁用
    RIGHTS_QUANTITY_NULL(4003, "权益下发数量不能为空","权益下发数量不能为空"),
    COUPON_CHARGE_OFF_PARAM_ERR(4004, "优惠券核销请求参数异常","优惠券核销请求参数异常"),
    COUPON_CHARGE_OFF_TRIAL_NOT_MATCH_ERR(4005, "优惠券核销与试算不一致异常","优惠券核销与试算不一致异常"),
    COUPON_RIGHTS_INTERESTS_NOT_EFFECTIVE(4006,"权益未生效","权益未生效"),
    ACTIVITY_TO_ABLE(4007,"活动待生效","活动待生效"),
    ACTIVITY_DISABLED(4008, "活动已下线", "活动已下线"),
    ACTIVITY_RIGHTS_QUANTITY_MISMATCHED(4009, "权益下发数量与权益ID数量不一致", "权益下发数量与权益ID数量不一致"),
    ACTIVITY_QUANTITY_ZERO(4010,"活动人数已满","活动人数已满"),
    BANK_NO_EXIT_ERROR(4011,"银行机构不存在","银行机构不存在"),

    LOGOUT_CODE_ERROR(4012,"API-SERVER 的服务响应异常，请稍后再试","API-SERVER 的服务响应异常，请稍后再试"),

    //限流熔断降级相关的异常码值
    SENTINE_EXCEPTION_ERROR(8001,"系统繁忙，请稍后再试！"),

    FUND_ID_NOT_EMPTY(3127, "联合贷放款资金源id不能为空", "联合贷放款资金源id不能为空"),

    INVESTOR_STATUS_NOT_NORMAL(3128, "出资方[%s]状态不正常", "出资方[%s]状态不正常"),

    INVESTOR_NOT_CREDIT(3129, "出资方[%s]没有授信记录", "出资方[%s]没有授信记录"),

    INVESTOR_AMOUNT_WRONG(3130, "出资方[%s]借款额度小于最小额度或者大于最大额度", "出资方[%s]借款额度小于最小额度或者大于最大额度"),

    INVESTOR_AMOUNT_EXCEPTION(3131, "出资方[%s]额度占用异常", "出资方[%s]额度占用异常"),

    INVESTOR_AMOUNT_FREEZE_FAIL(3132, "出资方[%s]额度冻结失败", "出资方[%s]额度冻结失败"),

    INVESTOR_LOAN_APPROVE_FAIL(3133, "出资方[%s]放款审核失败", "出资方[%s]放款审核失败"),

    CUSTOMER_INFO_ERROR(3134, "客户信息不全，检查是否激活", "客户信息不全，检查是否激活"),

    LOG_OFF_ERROR(3135, "兴安花卡销户失败", "兴安花卡销户失败"),

    //批量异常
    BATCH_MERGE_BILLING_AMOUNT_WRONG(3200,"贷记金额与借记金额不等"),

    //核销错误信息
    CHARGEOFF_DATA_EXISTS(3201, "订单已核销", "订单已核销：3201"),

    BLOCK_CODE_REJECT(3401,"封锁码拒绝","封锁码拒绝：3401"),
    ;

    private int code;
    private String msg;
    /**
     * 接口对外提示语
     */
    private String userTips;

    TxnRespCode(int c, String msg) {
        this.code = c;
        this.msg = msg;
    }

    TxnRespCode(int code, String msg, String userTips) {
        this.code = code;
        this.msg = msg;
        this.userTips = userTips;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getUserTips() {
        if(null == userTips){
            return msg;
        }
        return userTips;
    }

    public String getMsg(String errorType) {
        return errorType + msg;
    }

    public String getFormatMsg(Object... args) {
        return String.format(this.msg, args);
    }

    public String getFormatUserTips(Object... args) {
        return String.format(this.userTips, args);
    }

    /**
     * 根据 code 查找枚举
     * @param code
     * @return
     */
    public static TxnRespCode getByCode(int code){
        TxnRespCode[] txnRespCodes = TxnRespCode.values();
        return Arrays.stream(txnRespCodes).filter(item -> item.getCode() == code).findFirst().orElse(ERROR);
    }
}